package com.example.demo;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.ProtocolException;

import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class HtmlDownloader {

	public static String downloadHtmlFromUrl(String urlString) {
        StringBuilder htmlContent = new StringBuilder();
        CloseableHttpClient httpClient = HttpClients.createDefault();
		try {
			HttpGet httpPost = new HttpGet(urlString);

	        CloseableHttpResponse response = httpClient.execute(httpPost);
            System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

            String result = EntityUtils.toString(response.getEntity(), "UTF-8");
            htmlContent.append(result);
			
			
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (ProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

        // 返回读取到的HTML内容
        return htmlContent.toString().trim();
    }
}

